<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {


	public function index(){
		$post = $this->input->post('submit');
		if($post){

			$this->load->model('member');
		
			$this->load->library('form_validation');

			$this->form_validation->set_rules('NIM', 'NIM', 'required');
			$this->form_validation->set_rules('password', 'Password', 'required');

			if ($this->form_validation->run() == FALSE)
			{
				$data ['error'] = validation_errors ();
			} else {
				$NIM = $this ->input->post('NIM');
				$password = $this->input->post('password');
				$userData = $this->member->cek_member_login($NIM,$password);
				

				if(!empty($userData)){
					$sessionData['id'] = $userData['id_member'];
					$sessionData['nama'] = $userData['nama'];
					$sessionData['NIM'] = $userData['nim'];
					$sessionData['password'] = $userData['password'];
					$sessionData['divisi'] = $this->member->getDivisi($userData['id_divisi']);
					$sessionData['photo'] = $this->member->getPhoto($userData['nim']);
					$sessionData['email'] = $userData['email'];
					$sessionData['telp'] = $userData['telp'];
					$sessionData['alamat'] = $userData['alamat'];
					$sessionData['is_login'] = TRUE;

					$this->session->set_userdata($sessionData);
					
					redirect('web/member');

				}else{
					$data['error'] = 'User dan Password tidak sesuai,<br />silahkan periksa kembali';
				}	
			}
		}else{

			$data['error']   = $this->session->flashdata('error');			
		}	

		$this->load->view('web/login', $data);

	}

}	